﻿//**************************************************
//Description:  格子系统
//Author:       leubooth
//Date:         2016.11.02
//**************************************************

using UnityEngine;
using System.Collections.Generic;

public class GridSystem : MonoBehaviour
{
    /// <summary>
    /// 单件
    /// </summary>
    public static GridSystem Instance;

    /// <summary>
    /// 网格预制
    /// </summary>
    public GameObject gridPrefab;

    /// <summary>
    /// 网格X轴格子数
    /// </summary>
    public int countX = 9;

    /// <summary>
    /// 网格Y轴格子数
    /// </summary>
    public int countY = 9;

    /// <summary>
    /// 格子列表
    /// </summary>
    public List<Grid> orderGrids = new List<Grid>();

    /// <summary>
    /// 生成元素的位置
    /// </summary>
    public Vector3 bornPos;

    /// <summary>
    /// 元素运动类型
    /// 0   下落
    /// 1   螺旋
    /// 2   S形
    /// </summary>
    public int moveType;

    void Awake()
    {
        Instance = this;
    }

    /// <summary>
    /// 初始化格子系统
    /// </summary>
    public void InitGridSystem()
    {
        for (int i = 0; i < countX; i++)
        {
            for (int j = 0; j < countY; j++)
            {
                GameObject go = (GameObject)Instantiate(gridPrefab);
                go.transform.position = new Vector3(i, j, 1.0f);
                //将格子系统生成为黑白棋盘格
                if ((i % 2 + j % 2) == 1)
                {
                    go.GetComponent<MeshRenderer>().material.color = Color.gray;
                }
                go.transform.parent = this.transform;

                Grid grid = go.GetComponent<Grid>();
                grid.x = i;
                grid.y = j;
            }
        }
    }

    /// <summary>
    /// 初始化格子系统
    /// </summary>
    /// <param name="moveType"></param>
    public void InitGridSystem(int moveType)
    {
        //moveType = 0 下落
        //moveType = 1 左下角开始螺旋至中心
        //moveType = 2 左下角开始S形至右上角
        //列表的压缩规则从上游到下游

        //TODO...用配置表将运动轨迹生成出来

        //清空list
        orderGrids.Clear();

        //清空格子
        for (int m = 0; m < transform.childCount; m++)
        {
            Destroy(transform.GetChild(m).gameObject);
        }

        switch (moveType)
        {
            case 1:
                for (int i = 0; i < countX; i++)
                {
                    for (int j = countY - 1; j >= 0; j--)
                    {
                        GameObject go = (GameObject)Instantiate(gridPrefab);
                        go.transform.position = new Vector3(i, j, 1.0f);
                        //将格子系统生成为黑白棋盘格
                        //if ((i % 2 + j % 2) == 1)
                        //{
                        //    go.GetComponent<MeshRenderer>().material.color = Color.gray;
                        //}
                        go.transform.parent = this.transform;

                        Grid grid = go.GetComponent<Grid>();
                        grid.x = i;
                        grid.y = j;

                        //将元素压到List中代表运动轨迹
                        orderGrids.Add(grid);
                    }
                }
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

    /// <summary>
    /// 读取关卡配置生成格子系统
    /// </summary>
    /// <param name="levelName"></param>
    public void InitGridSystem(string levelName)
    {
        //moveType = 0 下落
        //moveType = 1 左下角开始螺旋至中心
        //moveType = 2 左下角开始S形至右上角
        //列表的压缩规则从上游到下游

        //TODO...用配置表将运动轨迹生成出来

        //清空list
        orderGrids.Clear();

        //清空格子
        for (int m = 0; m < transform.childCount; m++)
        {
            Destroy(transform.GetChild(m).gameObject);
        }

        LevelBase levelBase = LevelDataManager.ReadLevelData(levelName);

        if (levelBase != null)
        {
            moveType = levelBase.direction;
        }

        bornPos = levelBase.born;

        if (levelBase.path.Count > 0)
        {
            for (int i = 0; i < levelBase.path.Count; i++)
            {
                GameObject go = (GameObject)Instantiate(gridPrefab);
                //go.transform.position = levelBase.path[i];
                go.transform.position = new Vector3(levelBase.path[i].x, levelBase.path[i].y,1.0f);
                go.transform.parent = this.transform;

                Grid grid = go.GetComponent<Grid>();
                grid.x = (int)levelBase.path[i].x;
                grid.y = (int)levelBase.path[i].y;

                //将元素压到List中代表运动轨迹
                orderGrids.Add(grid);
            }

            //运算需要将列表反转
            orderGrids.Reverse();
        }
        else
        {
            for (int i = 0; i < countX; i++)
            {
                for (int j = countY - 1; j >= 0; j--)
                {
                    GameObject go = (GameObject)Instantiate(gridPrefab);
                    go.transform.position = new Vector3(i, j, 1.0f);
                    go.transform.parent = this.transform;

                    Grid grid = go.GetComponent<Grid>();
                    grid.x = i;
                    grid.y = j;

                    //将元素压到List中代表运动轨迹
                    orderGrids.Add(grid);
                }
            }
        }
    }
}